<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb20">
      <!--      TODO: 新增按钮权限控制-->
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['business:houseLock:create']"
        >添加维修项目
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="项目类型" prop="projectType">
        <el-select v-model="queryParams.projectType" placeholder="请选择" clearable style="width: 240px"
                   @change="handleQuery"
        >
          <el-option v-for="(item, index) in projectTypeMapping" :key="index" :label="item" :value="index">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="项目名称" prop="maintenanceItemName">
        <el-input v-model="queryParams.maintenanceItemName" placeholder="请输入" clearable style="width: 240px"
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属小区" prop="estateId">
        <el-select v-model="queryParams.estateId" placeholder="请选择" clearable filterable style="width: 240px"
                   @change="handleQuery"
        >
          <el-option v-for="(item, index) in estateList" :key="index" :label="item.estateName" :value="item.estateId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="list">
      <el-table-column label="序号" align="center" key="lockName" prop="lockName" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="项目类型" align="center" key="projectType" prop="projectType"
                       :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          {{ projectTypeMapping[scope.row.projectType] }}
        </template>
      </el-table-column>
      <el-table-column label="所属小区" align="center" key="estateName" prop="estateName"
                       :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          {{ scope.row.estateName ? scope.row.estateName : '-' }}
        </template>
      </el-table-column>
      <el-table-column label="维修项目" align="center" key="maintenanceItemName" prop="maintenanceItemName"
                       :show-overflow-tooltip="true"
      />
      <el-table-column label="材料费" align="center" prop="materialCost">
        <template slot-scope="scope">
          <span v-if="scope.row.materialCostType == 2">由维修员根据实际情况填写</span>
          <span v-else>{{ scope.row.materialCost ? scope.row.materialCost : '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="劳务费" align="center" prop="laborCost">
        <template slot-scope="scope">
          <span v-if="scope.row.laborCostType == 2">由维修员根据实际情况填写</span>
          <span v-else>{{ scope.row.laborCost ? scope.row.laborCost : '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="text" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"
    />

    <!--  添加维修项目弹出层   -->
    <Add v-model="open" :title="title" :data="form" @success="getList"/>
  </div>
</template>

<script>
import { listMaintenance, deleteMaintenance } from '@/api/houseManagement/maintenance'
import { listMaintenanceItem, getEstateList, deleteMaintenanceItem } from '@/api/houseManagement/maintenanceItem'
import Add from './components/add.vue'
import { debounce } from '@/utils'

export default {
  name: 'Index',
  dicts: ['dict_sweep_state', 'sweep_type'],
  components: {
    Add
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: null,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        maintenanceItemName: undefined,
        estateId: undefined,
        projectType: undefined,
        chargeItemType: '1' //类型(1.维修,2.保洁)
      },
      // 小区列表
      estateList: [],
      //项目类型映射
      projectTypeMapping: {
        '1': '指定小区',
        '2': '公开通用'
      }
    }
  },
  watch: {},
  created() {
    this.getList()
    this.getEstateList()
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true
      listMaintenanceItem(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.list = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 获取小区列表 */
    getEstateList() {
      getEstateList().then(response => {
        this.estateList = response.data
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        personName: undefined,
        personPhone: undefined
      },
        this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true
      this.title = '添加维修项目'
      this.form = {}
    },
    handleEdit(row) {
      this.title = '编辑维修项目'
      this.form = { ...row }
      this.open = true
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('此操作将删除该项目, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteMaintenanceItem(row.maintenanceItemId).then(() => {
          this.$message.success('删除成功')
          this.getList()
        })
      }).catch(() => {
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
